const { getIntegralRecordPage } = require('../../utils/Integral')


Page({
  data: {
    records: [],
    current: 1,
    size: 20,
    hasMore: true,
    loading: false,
    totalSum: 0
  },
  onLoad() {
    this.loadMore();
  },
  loadMore() {
    if (!this.data.hasMore || this.data.loading) return;
    this.setData({ loading: true });
    getIntegralRecordPage(this.data.current, this.data.size).then(res => {
      const newRecords = (res.records || res.data || []).map(item => {
        item.integralTime = this.formatDateTime(item.integralTime);
        return item;
      });
      const total = res.total || (res.data && res.data.total) || 0;
      const allRecords = this.data.records.concat(newRecords);
      this.setData({
        records: allRecords,
        current: this.data.current + 1,
        hasMore: allRecords.length < total,
        loading: false,
        totalSum: allRecords.length || 0
      });
    }).catch(() => {
      this.setData({ loading: false });
    });
  },
  onScrollToLower() {
    this.loadMore();
  },
  formatDateTime(str) {
    if (!str) return '';
    const d = new Date(str);
    const pad = n => n < 10 ? '0' + n : n;
    return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
  }  
}); 